package com.lrkj.workorder.service.impl;

import com.lrkj.common.core.domain.TreeSelectUp;
import com.lrkj.common.core.domain.entity.RepairProject;
import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.StringUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.workorder.mapper.RepairProjectMapper;
import com.lrkj.workorder.service.IRepairFeeScaleService;
import com.lrkj.workorder.service.IRepairProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 报修项目Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-04-22
 */
@Service
public class RepairProjectServiceImpl implements IRepairProjectService {
    @Autowired
    private RepairProjectMapper repairProjectMapper;

    @Autowired
    private IRepairFeeScaleService repairFeeScaleService;

    /**
     * 查询报修项目
     *
     * @param id 报修项目ID
     * @return 报修项目
     */
    @Override
    public RepairProject selectRepairProjectById(String id) {
        return repairProjectMapper.selectRepairProjectById(id);
    }

    /**
     * 查询报修项目列表
     *
     * @param repairProject 报修项目
     * @return 报修项目
     */
    @Override
    public List<RepairProject> selectRepairProjectList(RepairProject repairProject) {
        return repairProjectMapper.selectRepairProjectList(repairProject);
    }

    /**
     * 新增报修项目
     *
     * @param repairProject 报修项目
     * @return 结果
     */
    @Override
    public int insertRepairProject(RepairProject repairProject) {
        repairProject.setId(IdUtils.simpleUUID());
        repairProject.setCreateTime(DateUtils.getNowDate());
        this.setInfo(repairProject);
        return repairProjectMapper.insertRepairProject(repairProject);
    }

    /**
     * 修改报修项目
     *
     * @param repairProject 报修项目
     * @return 结果
     */
    @Override
    public int updateRepairProject(RepairProject repairProject) {
        repairProject.setUpdateTime(DateUtils.getNowDate());
        this.setInfo(repairProject);
        return repairProjectMapper.updateRepairProject(repairProject);
    }

    private void setInfo(RepairProject bean) {
        if (!bean.getParentId().equals("0")) {
            RepairProject parentBean = repairProjectMapper.selectRepairProjectById(bean.getParentId());
            bean.setAncestors(parentBean.getAncestors() + "," + bean.getParentId());
        } else {
            bean.setAncestors("0");
        }
    }

    /**
     * 批量删除报修项目
     *
     * @param ids 需要删除的报修项目ID
     * @return 结果
     */
    @Override
    public int deleteRepairProjectByIds(String[] ids) {
        StringBuffer buffer = new StringBuffer();
        RepairProject eg = new RepairProject();
        eg.getParams().put("ids", ids);
        List<RepairProject> repairProjects = repairProjectMapper.selectRepairProjectList(eg);
        repairProjects.stream().forEach(row -> {
            int num = repairProjectMapper.juniorDataCount(row.getId());
            if (num > 0) {
                buffer.append(row.getProjectName() + ",");
            }
        });
        if (buffer.length() > 0) {
            buffer.deleteCharAt(buffer.length() - 1);
            throw new BaseException("报修项目：" + buffer.toString() + " 存在下级项目。");
        }
        return repairProjectMapper.deleteRepairProjectByIds(ids);
    }

    /**
     * 删除报修项目信息
     *
     * @param id 报修项目ID
     * @return 结果
     */
    @Override
    public int deleteRepairProjectById(String id) {
        return repairProjectMapper.deleteRepairProjectById(id);
    }

    @Override
    public List<TreeSelectUp> buildDeptTreeSelect(List<RepairProject> repairProjects) {

        List<RepairProject> repairTrees = buildDeptTree(repairProjects);
        return repairTrees.stream().map(TreeSelectUp::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param depts 部门列表
     * @return 树结构列表
     */
    public List<RepairProject> buildDeptTree(List<RepairProject> depts) {
        List<RepairProject> returnList = new ArrayList<RepairProject>();
        List<String> tempList = new ArrayList<String>();
        for (RepairProject dept : depts) {
            tempList.add(dept.getId());
        }
        for (Iterator<RepairProject> iterator = depts.iterator(); iterator.hasNext(); ) {
            RepairProject dept = (RepairProject) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId())) {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty()) {
            returnList = depts;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<RepairProject> list, RepairProject t) {
        // 得到子节点列表
        List<RepairProject> childList = getChildList(list, t);
        t.setChildren(childList);
        for (RepairProject tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<RepairProject> getChildList(List<RepairProject> list, RepairProject t) {
        List<RepairProject> tlist = new ArrayList<RepairProject>();
        Iterator<RepairProject> it = list.iterator();
        while (it.hasNext()) {
            RepairProject n = (RepairProject) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<RepairProject> list, RepairProject t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    /**
     * 批量新增报修项目信息
     *
     * @param repairProjects 报修项目对象
     */
    public int batchAddData(List<RepairProject> repairProjects) {
        return repairProjectMapper.batchAddData(repairProjects);
    }
}
